<template>
  <section class="page-content">
    <div class="toolbar">
      <el-form :inline="true">
        <el-form-item>
          <el-button type="success" size="small" icon="el-icon-plus" @click="create">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="tableData"
      border>
      <el-table-column
        prop="name"
        width="280"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="condition"
        width="280"
        label="升级金额(元)">
      </el-table-column>
      <el-table-column
        prop="discount"
        label="折扣">
      </el-table-column>
      <el-table-column
        prop="discount_desc"
        label="折扣名称">
      </el-table-column>
      <el-table-column
        fixed="right"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editRow(scope.row)"
            type="default"
            size="small">
            编辑
          </el-button>
          <!-- <el-button
            @click.native.prevent="deleteRow(scope.row)"
            type="danger"
            size="small">
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- dialog -->
    <el-dialog class="dialog" title="等级" :visible.sync="visible">
      <el-form :model="formData" ref="formData" :rules="formRules"  label-width="100px">
        <el-form-item label="等级名称" prop="name">
          <el-input v-model="formData.name" placeholder="等级名称"></el-input>
        </el-form-item>
        <el-form-item label="升级金额" prop="condition">
          <el-input type="number" v-model="formData.condition" placeholder="升级金额"></el-input>
        </el-form-item>
        <el-form-item label="折扣" prop="discount">
          <el-input type="number"  v-model="formData.discount" placeholder="折扣"></el-input>
        </el-form-item>
        <el-form-item label="折扣名称" prop="discount_desc">
          <el-input v-model="formData.discount_desc" placeholder="折扣描述"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false" :disabled="editLoading">取 消</el-button>
        <el-button type="primary" :loading="editLoading" @click="handleCreateOrEdit('formData')">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
  export default {
    data () {
      return {
        loading: false,
        tableData: [],
        visible: false,
        editLoading: false,
        tempData: {
          id: null,
          name: '',
          condition: 1000,
          discount: 1,
          discount_desc: ''
        },
        formData: {},
        formRules: {
          name: [
            { required: true, message: '请输入等级名称', trigger: 'blur' },
            { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
          ],
          condition: [
            { required: true, message: '请输入升级消费最低金额', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '请输入享受的折扣', trigger: 'blur' }
          ],
          discount_desc: [
            { required: true, message: '请输入折扣描述', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this._restFormData()
      this._getData()
    },
    methods: {
      create () {
        this._restFormData()
        this.visible = true
      },
      editRow (item) {
        this.formData = Object.assign({}, item)
        this.visible = true
      },
      handleCreateOrEdit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http({
              method: this.formData.id === null ? 'post' : 'put',
              url: this.formData.id === null ? '/levels' : '/levels/' + this.formData.id,
              data: this.formData
            }).then((response) => {
              this.$message.success(response.data.message)
              this.editLoading = false
              this.visible = false
              this._getData()
            }).catch((error) => {
              this.editLoading = false
              console.log(error)
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      _restFormData () {
        this.formData = Object.assign({}, this.tempData)
      },
      _getData () {
        this.loading = true
        this.$http.get('/levels').then((response) => {
          this.loading = false
          this.tableData = response.data
        }).catch((error) => {
          this.loading = false
          console.log(error)
        })
      }
    }
  }
</script>
 